package com.sz.biz.logistics.core.service.impl;

import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.core.entity.PriceFreightRegionSetting;
import com.sz.biz.logistics.core.service.PriceFreightRegionSettingService;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xutao on 17/3/23.
 */
@Service("PriceFreightRegionSettingService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PriceFreightRegionSettingServiceImpl extends AbstractService implements PriceFreightRegionSettingService {
    @Override
    public int save(PriceFreightRegionSetting entry) {
        dao.save(getSqlName("insertSelective"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_FREIGHT_REGION_SETTING", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_FREIGHT_REGION_SETTING, UserActions.ADD, "添加运输重量段价格设置", dataBackup);
        return entry.getId();
    }

    @Override
    public void batchsave(List<PriceFreightRegionSetting> list) {
        dao.batchInsert(getSqlName("insertSelective"), list);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_FREIGHT_REGION_SETTING", list);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_FREIGHT_REGION_SETTING, UserActions.ADD, "添加运输重量段价格设置", dataBackup);

    }

    @Override
    public List<PriceFreightRegionSetting> findByQuotationSetId(int quotationSetId) {
        ParamData pd = new ParamData();
        pd.put("quotationSetId", quotationSetId);
        return dao.findForList(getSqlName("findByQuotationSetId"), pd, PriceFreightRegionSetting.class);
    }

    /**
     * 根据主键Id查询单个实体
     *
     * @param quotationId quotationId
     * @return 查询出来的实体对象
     */
    @Override
    public List<PriceFreightRegionSetting> findByQuotationId(Integer quotationId) {
        ParamData pd = new ParamData();
        pd.put("quotationId", quotationId);
        return dao.findForList(getSqlName("findByQuotationId"), pd, PriceFreightRegionSetting.class);
    }

    /**
     * 根据quotationId删除
     *
     * @param quotationId quotationId
     */
    @Override
    public void deletedByQuotationId(Integer quotationId) {
        dao.delete(getSqlName("deletedByQuotationId"), quotationId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_FREIGHT_REGION_SETTING", quotationId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_FREIGHT_REGION_SETTING,
                UserActions.DELETE, "删除运输重量段价格设置", dataBackup);
    }


    /**
     * 根据quotationId删除起点，用于价格下线编辑价格时直接删除
     *
     * @param quotationSetId quotationSetId
     */
    @Override
    public void deletedByQuotationSetId(Integer quotationSetId) {
        dao.delete(getSqlName("deletedByQuotationSetId"), quotationSetId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_FREIGHT_REGION_SETTING", quotationSetId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_FREIGHT_REGION_SETTING, UserActions.DELETE, "删除运输重量段价格设置", dataBackup);
    }


    @Override
    protected String getMapperNamespace() {
        return "PriceFreightRegionSettingMapper";
    }
}
